package org.springblade.modules.repair.vo;

import cn.hutool.core.convert.Convert;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.repair.entity.MaintainPlanTemplate;
import org.springblade.modules.repair.enums.EmerProcTypeEnum;
import org.springblade.modules.repair.enums.MaintainPlanContentEnum;
import org.springblade.modules.repair.enums.WorkOrderTypeEnum;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Date 14:24 2023/10/31
 * @Description {
 *   电梯档案工单列表
 * }
 * @Author lsay
 **/
@Data
public class ElevatorFilesPlanListVO {

	/**
	 * 工单id
	 */
	private Long planId;

	/**
	 * 工单类型
	 */
	private Integer typeStatus;

	/**
	 * 工单类型名称
	 */
	private String typeStatusName;

	/**
	 * 工单完成时间
	 */
	private Date planEndTime;

	/**
	 * 工单状态
	 */
	private Integer status;

	/**
	 * 工单状态名称
	 */
	private String statusName;



	/**
	 * 模板名称
	 */
	private String templateName;

	/**
	 * 模板名称
	 */
	private List<String> templateNames;

	/**
	 * 逾期天数
	 */
	private Integer overdueDay;

	/**
	 * 逾期描述
	 */
	private String overdueDayName;

	/**
	 * 延期类型
	 */
	private Integer deferType;

	/**
	 * 延期类型描述
	 */
	private String deferTypeName;

	/**
	 * 故障描述
	 */
	private String problemItem;

	/**
	 * 故障描述
	 */
	private List<String> problemItemNames;

	/**
	 * 故障描述 其他
	 */
	private String problemOtherDesc;

	/**
	 * 部件名称
	 */
	private String partsName;

	/**
	 * 部件数量
	 */
	private String partsNum;

	/**
	 * 报警类型
	 */
	private Integer trapType;

	/**
	 * 报警类型名称
	 */
	private String trapTypeName;

	/**
	 * 报警备注
	 */
	private String trapDesc;

	/**
	 * 及时性 1-及时 2-逾期
	 */
	private String timeliness;

	/**
	 * 时长 1-过长 2-过短
	 */
	private String duration;

	private Integer workMinute;

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	private Date planDate;

	private Date maintainSignatureTime;

	private String userName;

	private List<String> userNames;

	private Integer auditType;

	private Date planOverdueDate;

	private Date sortDate;

	/**
	 * 部件名称集合
	 */
	private String partsNames;

	/**
	 * 紧急程度
	 */
	private Integer emerProcType;

	/**
	 * 工单类型
	 */
	private Integer workOrderType;

	/**
	 * 紧急程度描述
	 */
	private String emerProcTypeName;

	/**
	 * 工单类型描述
	 */
	private String workOrderTypeName;


	/**
	 * 质检和事务工单描述
	 */
	private String planDesc;

	/**
	 * 整改事项统计
	 */
	private Integer scoreIssue;

	public void setPartsNames(String partsNames) {
		if(Func.isNotEmpty(partsNames)){
			this.partsNames = partsNames.replace("&amp;&amp;",">");
		}
	}

	public void setEmerProcType(Integer emerProcType) {
		if(Func.isNotEmpty(emerProcType)){
			this.emerProcTypeName = EmerProcTypeEnum.getName(emerProcType);
		}
		this.emerProcType = emerProcType;
	}

	public void setWorkOrderType(Integer workOrderType) {
		if(Func.isNotEmpty(workOrderType)){
			this.workOrderTypeName = WorkOrderTypeEnum.getName(workOrderType);
		}
		this.workOrderType = workOrderType;
	}

	public void setUserName(String userName) {
		this.userName = userName;
		if(Func.isNotEmpty(userName)){
			this.userNames = Func.toStrList(userName);
		}
	}

	public void setTypeStatus(Integer typeStatus) {
		this.typeStatus = typeStatus;
		if(Func.isNotEmpty(typeStatus)){
			this.typeStatusName = MaintainPlanContentEnum.getName(typeStatus);
		}
	}

	public void setTrapType(Integer trapType) {
		this.trapType = trapType;
		if(Func.isNotEmpty(trapType)){
			this.trapTypeName = trapType == 0 ? "困人":"伤人";
		}
	}

	public void setTemplateName(String templateName) {
		if(Func.isNotEmpty(templateName) && templateName.contains("%")){
			List<String> strings = Func.toStrList(templateName);
			List<MaintainPlanTemplate> list = new ArrayList<>(10);
			for (String str :strings) {
				String[] split = str.split("%");
				MaintainPlanTemplate maintainPlanTemplate = new MaintainPlanTemplate();
				maintainPlanTemplate.setTemplateName(split[1]);
				maintainPlanTemplate.setSort(Convert.toInt(split[2],0));
				list.add(maintainPlanTemplate);
			}
			List<String> collect = list.stream().sorted(Comparator.comparing(MaintainPlanTemplate::getSort)).map(MaintainPlanTemplate::getTemplateName).collect(Collectors.toList());
			this.templateName = String.join(",",collect);
			this.templateNames = collect;
		}else{
			this.templateName = templateName;
		}
	}
}
